home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 November: Tool Chest / Dev.CD Nov 94.toast / Sample Code / Snippets / Development Tools & Languages / MouseInfo 1.0.1 / UMouseTrackBehavior.cp < prev    next >
Encoding:
Text File  |  1992-07-15  |  5.7 KB  |  217 lines  |  [TEXT/MPS ]

  1. //     UMouseTrackBehavior.cp 
  2. //     Copyright © 1992 by Apple Computer, Inc. All rights reserved.
  3. //    Kent Sandvik DTS
  4. //    This file contains the basic TMouseTrackBehavior member functions
  5. //    Version Info (latest first):
  6. //
  7. //    <1>        khs        1.0        First final version
  8. //    <2>        khs        1.0.1    Fixed a memory leak in TMapApplication::GetSleepValue()
  9.  
  10.  
  11. #ifndef __MOUSETRACKBEHAVIOR__
  12. #include "UMouseTrackBehavior.h"
  13. #endif
  14.  
  15.  
  16. //    Initialize needed parts for the MouseTrackBehavior modules
  17. #pragma segment AInit
  18. pascal void InitMouseTrackBehavior()
  19. {
  20.     if (gDeadStripSuppression)
  21.     {
  22.         macroDontDeadStrip(TMouseTrackBehavior);
  23.         macroDontDeadStrip(TTrackWindow);
  24.     }
  25.     
  26.     RegisterStdType("TTrackWindow", 'ttrk');
  27. }
  28.  
  29.  
  30. //    Close the remove the single swallow application when closing the floating window
  31. #pragma segment AClose
  32. pascal void TTrackWindow::Close()
  33. {
  34.     gMouseTrackWindow = NULL;                    // signal that it's OK to open one again
  35.  
  36.     gApplication->RemoveBehavior(fBehavior);    //    Get rid of the TSwallowBehavior in gApplication...
  37.  
  38.     inherited::Close();
  39. }
  40.  
  41.  
  42. //    Get the pointer to the single swallow behavior which we need when removing it    
  43. #pragma segment ARes
  44. pascal void TTrackWindow::GetOriginatorBehavior(TSwallowBehavior* behavior)
  45. {
  46.     fBehavior = behavior;
  47. }
  48.  
  49.  
  50. //     Create behavior and define default values
  51. #pragma segment AInit
  52. pascal void TMouseTrackBehavior::IMouseTrackBehavior(ResNumber menuID)
  53. {
  54.     this->IBehavior(kMouseTrackBehavior);
  55.     this->SetIdleFreq(0);                        // call as often as possible
  56.     this->SetEnabled(TRUE);                        // enable behavior    
  57.     fMenuID = menuID;
  58. }
  59.  
  60.  
  61. //    Initialized any static fields in the behavior
  62. #pragma segment AInit
  63. pascal void TMouseTrackBehavior::Initialize()
  64. {
  65.     inherited::Initialize();
  66.     fMenuID = 0;
  67. }
  68.  
  69.  
  70. //    Setup needed menus dynamically, i.e. mouse tracking menu if floating window not present
  71. #pragma segment ARes
  72. pascal void TMouseTrackBehavior::DoSetupMenus()
  73. {
  74.     inherited::DoSetupMenus();
  75.     Enable(cOpenTracker, gMouseTrackWindow == NULL);
  76. }
  77.  
  78.  
  79. //    Do menu commands associated with the mousetrack behavior, in this case only one
  80. //    menu, open floating window and install the event swallowing behavior
  81. #pragma segment ASelCommand
  82. pascal void TMouseTrackBehavior::DoMenuCommand(CommandNumber theNum)
  83. {
  84.     if (theNum == cOpenTracker && gMouseTrackWindow == NULL)
  85.     {
  86.         this->CreateWindoid();                    // create windoid on the flight
  87.         this->CreateSwallowBehavior();            // create and install event behavior
  88.         gMouseTrackWindow->Open();                // open windoid on screen
  89.         Enable(cOpenTracker, FALSE);            // disable the menu entry
  90.     }
  91.     else
  92.         inherited::DoMenuCommand(theNum);
  93. }
  94.  
  95.  
  96. //    Create windoid, only one which is used by all open documents
  97. #pragma segment AInit
  98. pascal void TMouseTrackBehavior::CreateWindoid()
  99. {
  100.     FailInfo fi;
  101.     TTrackWindow * aWindow = NULL;
  102.  
  103.     if (fi.Try())
  104.     {
  105.         //    Create Windoid
  106.         aWindow = (TTrackWindow *)gViewServer->NewTemplateWindow(kFloatingWindow, NULL);
  107.         FailNIL(aWindow);
  108.         gMouseTrackWindow = aWindow;            // register the windoid globally
  109.  
  110.         fi.Success();
  111.     }
  112.     else
  113.         fi.ReSignal();
  114. }
  115.  
  116.  
  117. //    Create swallow behavior which will eat all events, also install it to the main
  118. //    gApplication framework and let the floating window let know about this behavior
  119. #pragma segment AInit
  120. pascal void TMouseTrackBehavior::CreateSwallowBehavior()
  121. {
  122.     //    Install swallow behavior which takes *every* event!
  123.     TSwallowBehavior * aSwallowBehavior = new TSwallowBehavior;
  124.     aSwallowBehavior->ISwallowBehavior();
  125.     gApplication->AddBehavior(aSwallowBehavior);
  126.     gMouseTrackWindow->GetOriginatorBehavior(aSwallowBehavior);// windoid needs to know the current behavior    
  127. }
  128.  
  129.  
  130. //    Initialize the swallow behavior, it should find out the resources to the floating
  131. //    window (fields) which it needs when it updates them
  132. #pragma segment AInit
  133. pascal void TSwallowBehavior::ISwallowBehavior()
  134. {
  135.     this->IBehavior(kNoIdentifier);
  136.     this->SetIdleFreq(0);                        // call as often as possible
  137.  
  138.     fTrackWindow = gMouseTrackWindow;            // get the global/only Windoid address
  139.  
  140.     //    Get Resource pointers
  141.     fMouseStatus = (TStaticText *)fTrackWindow->FindSubView('mous');
  142.     fVertical = (TNumberText *)fTrackWindow->FindSubView('numv');
  143.     fHorizontal = (TNumberText *)fTrackWindow->FindSubView('numh');
  144. }
  145.  
  146.  
  147. //    Get access to all events, and trigger actions based on key events, in our
  148. //    case mouseUp and mouseDowns
  149. #pragma segment ARes
  150. pascal Boolean TSwallowBehavior::DoToolboxEvent(TToolboxEvent* event)
  151. {
  152.     WindowPtr aWMgrWindow;
  153.  
  154.     short whereMouseDown = FindWindow(event->fEventRecord.where, aWMgrWindow);
  155.     TWindow * aWindow = gApplication->WMgrToWindow(aWMgrWindow);
  156.  
  157.     if (event && (event->fIdentifier == mouseDown))
  158.         fEventType = mouseDown;
  159.  
  160.     else if (event && (event->fIdentifier == mouseUp))
  161.         fEventType = mouseUp;
  162.  
  163.     else if (event && (event->fIdentifier == activateEvt))
  164.         fEventType = mouseUp;
  165.  
  166.     if (aWindow)
  167.     {
  168.         VPoint theMouse(event->fEventRecord.where);
  169.         aWindow->SuperToLocal(theMouse);
  170.         fMouseVPoint = theMouse;
  171.     }
  172.  
  173.     VRect dummy;
  174.     if (gMouseTrackWindow != NULL)
  175.         this->Draw(dummy);                        // only draw if windoid exists
  176.  
  177.     return inherited::DoToolboxEvent(event);
  178. }
  179.  
  180.  
  181. //    Draw contents inside floating window
  182. #pragma segment ASelCommand
  183. pascal void TSwallowBehavior::Draw(const VRect& area)
  184. {
  185.     if (gMouseTrackWindow != NULL)
  186.     {
  187.         //    Draw Mouse status
  188.         if (fEventType == mouseDown)
  189.         {
  190.             fMouseStatus->SetText("MouseDown", kRedraw);
  191.             fTrackWindow->Update();
  192.         }
  193.         else if (fEventType == mouseUp)
  194.         {
  195.             fMouseStatus->SetText("MouseUp", kRedraw);
  196.             fTrackWindow->Update();
  197.         }
  198.  
  199.         //    Draw coordinates
  200.         //    Avoid de-referencing problems by using stack based variables for NumToString
  201.         CStr255 tempH,  tempV;
  202.         VPoint tempVP;
  203.  
  204.         tempVP = fMouseVPoint;
  205.  
  206.         NumToString(tempVP.h, tempH);
  207.         NumToString(tempVP.v, tempV);
  208.         fHorizontal->SetText(tempH, kRedraw);
  209.         fVertical->SetText(tempV, kRedraw);
  210.         fTrackWindow->Update();
  211.     }
  212.  
  213.     inherited::Draw(area);
  214. }
  215.  
  216.  
  217.